package com.ultrapower.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ultrapower.commons.base.BaseController;
import com.ultrapower.commons.result.PageInfo;
import com.ultrapower.commons.utils.StringUtils;
import com.ultrapower.model.Task;
import com.ultrapower.service.ITaskService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sz
 * @since 2018-07-31
 */
@Controller
@RequestMapping("/task")
public class TaskController extends BaseController {

    @Autowired private ITaskService taskService;
    
    @GetMapping("/manager")
    public String manager() {
        return "admin/task/taskList";
    }
    
    @GetMapping("/product")
    public String product() {
        return "admin/task/product";
    }
    
    @GetMapping("/enterprise")
    public String enterprise() {
        return "admin/task/enterpriselist";
    }
    
    @PostMapping("/dataGrid")
    @ResponseBody
    public PageInfo dataGrid(String companyName,String type,Integer page, Integer limit, String sort, String order) {
    	sort = "id";
    	order = "asc";
        PageInfo pageInfo = new PageInfo(page, limit, sort, order);
        Map<String, Object> condition = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(companyName)) {
            condition.put("companyName", companyName);
        }
        System.out.println(type);
        if (StringUtils.isNotBlank(type)) {
            condition.put("type", type);
        }
        pageInfo.setCondition(condition);
        taskService.selectDataGrid(pageInfo);
        return pageInfo;
    }
    
    @PostMapping("/enterpriseGrid")
    @ResponseBody
    public PageInfo enterpriseGrid(String primaryP,String hotP,Integer page, Integer limit, String sort, String order) {
    	sort = "id";
    	order = "asc";
        PageInfo pageInfo = new PageInfo(page, limit, sort, order);
        Map<String, Object> condition = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(primaryP)) {
            condition.put("primaryP", primaryP);
        }
        System.out.println(hotP);
        if (StringUtils.isNotBlank(hotP)) {
            condition.put("hotP", hotP);
        }
        pageInfo.setCondition(condition);
        taskService.selectTaskEnterprise(pageInfo);
        return pageInfo;
    }
    
    /**
     * 添加页面
     * @return
     */
    @GetMapping("/addPage")
    public String addPage() {
        return "admin/task/taskAdd";
    }
    
    /**
     * 添加
     * @param 
     * @return
     */
    @PostMapping("/addBatch")
    @ResponseBody
    public Object addBatch(String primaryP,String hotP,String enterpriseIds) {
    	List<Task> tasks = new ArrayList<Task>();
    	for(String enterpriseId:enterpriseIds.split(";")) {
    	Task task = new Task();
        task.setCreateTime(new Date());
        task.setUpdateTime(new Date());
        task.setDeleteFlag(0);
        task.setPrimaryProduct(Integer.parseInt(primaryP));
        task.setHotProduct(Integer.parseInt(hotP));
        task.setEnterpriseId(Integer.parseInt(enterpriseId));
        task.setCreater(Integer.parseInt(getUserId().toString()));
        task.setType(1);
        tasks.add(task);
    	}
    	boolean b=taskService.insertBatch(tasks, tasks.size());
         if (b) {
             return renderSuccess("添加成功！");
         } else {
             return renderError("添加失败！");
         }
    }
    
    /**
     * 添加
     * @param 
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(@Valid Task task) {
        task.setCreateTime(new Date());
        task.setUpdateTime(new Date());
        task.setDeleteFlag(0);
        boolean b = taskService.insert(task);
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }
    
    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(int id) {
        boolean b = taskService.deleteById(id);
        if (b) {
            return renderSuccess("删除成功！");
        } else {
            return renderError("删除失败！");
        }
    }
    
    /**
     * 编辑
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editPage")
    public String editPage(Model model, Long id) {
        Task task = taskService.selectById(id);
        model.addAttribute("task", task);
        return "admin/task/taskEdit";
    }
    
    /**
     * 编辑
     * @param 
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public Object edit(@Valid Task task) {
        task.setUpdateTime(new Date());
        boolean b = taskService.updateById(task);
        if (b) {
            return renderSuccess("编辑成功！");
        } else {
            return renderError("编辑失败！");
        }
    }
}
